package com.qpf.mycms.manager.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qpf.mycms.api.LoginApi;
import com.qpf.mycms.api.PasswordServiceApi;
import com.qpf.mycms.api.SysUserServiceApi;
import com.qpf.mycms.api.exception.SystemException;
import com.qpf.mycms.api.exception.enums.ClientExceptionEnum;
import com.qpf.mycms.api.factory.PageFactory;
import com.qpf.mycms.api.factory.PageResultFactory;
import com.qpf.mycms.api.pojo.PageResult;
import com.qpf.mycms.api.pojo.UserRequest;
import com.qpf.mycms.api.pojo.dto.UserDto;
import com.qpf.mycms.manager.sys.domain.SysUser;
import com.qpf.mycms.manager.sys.mapper.SysUserMapper;
import com.qpf.mycms.manager.sys.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author shadaileng
 * &#064;description  针对表【user】的数据库操作Service实现
 * &#064;createDate  2024-07-28 16:28:58
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, LoginApi, SysUserServiceApi {

    @Resource
    private PasswordServiceApi passwordService;
    @Override
    public void add(UserRequest request) {
        SysUser user = new SysUser();
        checkAccount(request.getAccount());
        checkEmail(request.getEmail());
        checkMobile(request.getMobile());
        BeanUtils.copyProperties(request, user);
        user.setPassword(passwordService.encode(request.getPassword()));
        save(user);
    }

    /** 表单验证 BEG **/
    private void checkAccount(String account) {
        UserRequest request = new UserRequest();
        request.setAccount(account);
        if (queryCount(request) > 0) {
            throw new SystemException(ClientExceptionEnum.VALUE_EXISTED, "账号");
        }
    }
    private void checkEmail(String email) {
        UserRequest request = new UserRequest();
        request.setEmail(email);
        if (queryCount(request) > 0) {
            throw new SystemException(ClientExceptionEnum.VALUE_EXISTED, "邮箱");
        }
    }


    private void checkMobile(String mobile) {
        UserRequest request = new UserRequest();
        request.setMobile(mobile);
        if (queryCount(request) > 0) {
            throw new SystemException(ClientExceptionEnum.VALUE_EXISTED, "手机号码");
        }
    }

    /** 表单验证 END **/

    @Override
    public void del(UserRequest request) {
        findUser(request);
        removeById(request.getId());
    }

    @Override
    public void edit(UserRequest request) {
        SysUser user = findUser(request);
        Long id = user.getId();
        user = new SysUser();
        user.setId(id);
        BeanUtils.copyProperties(request, user);
        updateById(user);
    }


    @Override
    public SysUser detail(UserRequest request) {
        return findUser(request);
    }

    @Override
    public PageResult<SysUser> findPage(UserRequest request) {
        Page<SysUser> page = page(PageFactory.defaultPage(), createWrapper(request));
        return PageResultFactory.createPageResult(page);
    }
    private LambdaQueryWrapper<SysUser> createWrapper(UserRequest request) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(request)) {
            return wrapper;
        }
        wrapper.eq(!ObjectUtils.isEmpty(request.getId()), SysUser::getId, request.getId());
        wrapper.eq(!ObjectUtils.isEmpty(request.getAccount()), SysUser::getAccount, request.getAccount());
        wrapper.eq(!ObjectUtils.isEmpty(request.getMobile()), SysUser::getMobile, request.getMobile());
        wrapper.eq(!ObjectUtils.isEmpty(request.getEmail()), SysUser::getEmail, request.getEmail());
        return wrapper;
    }

    private SysUser findUser(UserRequest request) {
        if (ObjectUtils.isEmpty((request.getId()))) {
            SysUser user = this.getOne(createWrapper(request));
            if (ObjectUtils.isEmpty(user)) {
                throw new SystemException(ClientExceptionEnum.USER_NOT_EXITS);
            }
            return user;
        }

        SysUser user = getById(request.getId());
        if (ObjectUtils.isEmpty(user)) {
            throw new SystemException(ClientExceptionEnum.USER_NOT_EXITS);
        }
        return user;
    }
    public int queryCount(UserRequest request) {
        return this.count(createWrapper(request));
    }

    // ------------------------- 通用方法 -------------------------

    /**
     * 验证账号和密码的方法
     * @param username 账号
     * @param password 密码
     * @return UserDto
     */
    @Override
    public UserDto login(String username, String password) {
        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
            throw new SystemException(ClientExceptionEnum.PARAM_INCORRECT, "账号或密码");
        }
        UserDto userDto;
        SysUser user = getSysUserByAccount(username);
        if (ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(password) || !password.equals(user.getPassword()))  {
            throw new SystemException(ClientExceptionEnum.PARAM_INCORRECT, "账号或密码");
        }
        userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        return userDto;
    }

    private SysUser getSysUserByAccount(String account) {
        UserRequest request = new UserRequest();
        request.setAccount(account);
        return detail(request);
    }
    @Override
    public UserDto register(UserRequest request) {
        return null;
    }

    @Override
    public UserDto findUserByAccount(String account) {
        SysUser user = getSysUserByAccount(account);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(user,userDto);
        return userDto;
    }

}